package com.cmii.sjw.department.control.jsc.service.impl;

import cn.dev33.satoken.dao.SaTokenDao;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cmii.sjw.department.control.jsc.config.OneFormConfig;
import com.cmii.sjw.department.control.jsc.mapper.*;
import com.cmii.sjw.department.control.jsc.model.*;
import com.cmii.sjw.department.control.jsc.service.OneFormService;
import com.cmii.sjw.department.control.jsc.util.ApiDbHelper;
import com.cmii.sjw.department.control.jsc.util.Result;
import com.cmii.sjw.department.control.jsc.vo.DistrictKpiDTO;
import com.cmii.sjw.department.control.jsc.vo.DistrictKpiVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.utils.URIBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * @Author jfh
 * @Date 2025-02-25 周二 10:55
 * @Version 1.0
 */
@Slf4j
@Service
public class OneFormServiceImpl implements OneFormService {

    @Autowired
    private OneFormConfig ybtConfig;
    @Autowired
    private DistrictKpiMapper districtKpiMapper;
    @Autowired
    private ProfessionalCoursesMapper professionalCoursesMapper;
    @Autowired
    private SaTokenDao dao;

    private static final int TOKEN_EXPIRE_SECONDS = 7140;

    public String getToken() {
        String token = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/auth/getToken");
            uriBuilder.addParameter("appId", ybtConfig.getAppId());
            uriBuilder.addParameter("secretKey", ybtConfig.getSecretKey());
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();
            log.info("一表通Token ,url:{},apiResult:{}", url, apiResult);
            com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

            if ("0".equals(content.getObject("code", String.class))) {
                log.info("一表通Token结果:{}", content.getString("data"));
                token = content.getJSONObject("data").get("access_token").toString();
            } else {
                log.error("一表通Token结果异常 responseCode:{}", content.getString("code"));
            }
        } catch (Exception e) {
            log.error("一表通Token请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
        }
        return token;
    }

    @Override
    public String TokenManager() {
        String token = dao.get("one-form:token");
        if (StringUtils.isEmpty(token)) {
            synchronized (this) {
                token = dao.get("one-form:token"); // 双重检查
                if (StringUtils.isEmpty(token)) {
                    try {
                        token = getToken();
                        log.info("获取新的一表通Token:{}", token);
                        dao.set("one-form:token", token, TOKEN_EXPIRE_SECONDS);
                    } catch (Exception e) {
                        log.error("生成Token失败", e);
                        throw new RuntimeException("Token生成异常"); // 根据业务需求处理异常
                    }
                }
            }
        }
        return token;
    }

    /**
     * 查询区县列表
     *
     * @param year
     * @return
     */
    @Override
    public Result countyList(String year) {
        String url = ybtConfig.getUrl() + "/api/v1/data/countyList";
        // 参数
        String params = "?year=" + year;

        String apiResult = HttpUtil.createPost(url + params)
                .header(Header.AUTHORIZATION, TokenManager())
//                .form(params)
//                .body("")
                .execute()
                .body();
        log.info("一表通：区县列表 ,url:{} params:{},apiResult:{}", url, params, apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getObject("code", String.class))) {
            log.info("一表通：区县列表结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：区县列表结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }

    }

    @Override
    public Result highList(String year) {
        String url = ybtConfig.getUrl() + "/api/v1/data/highList";

        String params = "?year=" + year;

        String apiResult = HttpUtil.createPost(url + params)
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();
        log.info("一表通：高校列表 ,url:{} params:{},apiResult:{}", url, params, apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getObject("code", String.class))) {
            log.info("一表通：高校列表结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：高校列表结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    @Override
    public Result schoolList(String year, String schoolType, String search, String county, int pageNum, int pageSize) {
        String url = ybtConfig.getUrl() + "/api/v1/data/schoolList";

        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);
        if (schoolType != null && !schoolType.isEmpty()) {
            params.append("&schoolType=").append(schoolType);
        }
        if (search != null && !search.isEmpty()) {
            params.append("&search=").append(search);
        }
        if (county != null && !county.isEmpty()) {
            params.append("&county=").append(county);
        }
        params.append("&pageNum=").append(pageNum);
        params.append("&pageSize=").append(pageSize);

        String apiResult = HttpUtil.createPost(url + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：学校列表 ,url:{} params:{},apiResult:{}", url, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getObject("code", String.class))) {
            log.info("一表通：学校列表结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：学校列表结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    @Override
    public Result masterData(String year, String county) {
        String url = ybtConfig.getUrl() + "/api/v1/data/masterData";

        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);
        if (county != null && !county.isEmpty()) {
            params.append("&county=").append(county);
        }

        String apiResult = HttpUtil.createPost(url + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：专任教师数量 ,url:{} params:{},apiResult:{}", url, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getObject("code", String.class))) {
            log.info("一表通：专任教师数量结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：专任教师数量结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    @Override
    public Result masterDetail(String year, String county, int type) {
        String url = ybtConfig.getUrl() + "/api/v1/data/masterDetail";

        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);
        if (county != null && !county.isEmpty()) {
            params.append("&county=").append(county);
        }
        params.append("&type=").append(type);

        String apiResult = HttpUtil.createPost(url + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：专任教师数量详情 ,url:{} params:{},apiResult:{}", url, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getObject("code", String.class))) {
            log.info("一表通：专任教师数量详情结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：专任教师数量详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    @Override
    public Result qualification(String year, String county, String type) {
        String url = ybtConfig.getUrl() + "/api/v1/data/qualification";

        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);
        if (county != null && !county.isEmpty()) {
            params.append("&county=").append(county);
        }
        if (type != null && !type.isEmpty()) {
            params.append("&type=").append(type);
        }

        String apiResult = HttpUtil.createPost(url + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：任教师学历情况 ,url:{} params:{},apiResult:{}", url, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getObject("code", String.class))) {
            log.info("一表通：任教师学历情况结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：任教师学历情况结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    @Override
    public Result qualificationDetail(String year, String county, int type, String schoolType) {
        String url = ybtConfig.getUrl() + "/api/v1/data/qualificationDetail";

        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);
        if (county != null && !county.isEmpty()) {
            params.append("&county=").append(county);
        }
        params.append("&type=").append(type);
        if (schoolType != null && !schoolType.isEmpty()) {
            params.append("&schoolType=").append(schoolType);
        }

        String apiResult = HttpUtil.createPost(url + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：任教师学历情况详情 ,url:{} params:{},apiResult:{}", url, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getObject("code", String.class))) {
            log.info("一表通：任教师学历情况详情结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：任教师学历情况详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    @Override
    public Result age(String year, String county, String type) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/age";

        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);
        if (county != null && !county.isEmpty()) {
            params.append("&county=").append(county);
        }
        if (type != null && !type.isEmpty()) {
            params.append("&type=").append(type);
        }

        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：专任教师年龄情况 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：专任教师年龄情况结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：专任教师年龄情况结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    @Override
    public Result title(String year, String county, String type) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/title";

        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);
        if (county != null && !county.isEmpty()) {
            params.append("&county=").append(county);
        }
        if (type != null && !type.isEmpty()) {
            params.append("&type=").append(type);
        }

        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：专任教师职称情况 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：专任教师职称情况结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：专任教师职称情况结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    @Override
    public Result gender(String year, String type, String name) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/gender";

        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);

        if (type != null && !type.isEmpty()) {
            params.append("&type=").append(type);
        }
        if (name != null && !name.isEmpty()) {
            params.append("&name=").append(name);
        }
        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：专任教师性别比例 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：专任教师性别比例结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：专任教师性别比例结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    @Override
    public Result genderDetail(String year, String type, String name) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/genderDetail";

        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);

        if (type != null && !type.isEmpty()) {
            params.append("&type=").append(type);
        }
        if (name != null && !name.isEmpty()) {
            params.append("&name=").append(name);
        }
        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：专任教师性别比例 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：专任教师性别比例结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：专任教师性别比例结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result country(String year, String county, String type) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/country";
        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);
        if (county != null && !county.isEmpty()) {
            params.append("&county=").append(county);
        }
        if (type != null && !type.isEmpty()) {
            params.append("&type=").append(type);
        }
        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：专任教师城乡分布情况 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：专任教师城乡分布情况结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：专任教师城乡分布情况结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result countryDetail(String year, String county, String type) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/countryDetail";
        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);
        if (county != null && !county.isEmpty()) {
            params.append("&county=").append(county);
        }
        if (type != null && !type.isEmpty()) {
            params.append("&type=").append(type);
        }
        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：专任教师城乡分布情况详情 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：专任教师城乡分布情况详情结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：专任教师城乡分布情况详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result instructor(String year) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/instructor";
        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);

        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：高校辅导员教师数量 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：高校辅导员教师数量结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：高校辅导员教师数量结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result instructorDetail(String year, String schoolName) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/instructorDetail";
        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);

        if (schoolName != null && !schoolName.isEmpty()) {
            params.append("&schoolName=").append(schoolName);
        }

        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：高校辅导员教师数量详情 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：高校辅导员教师数量详情结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：高校辅导员教师数量详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result courses(String year, String type, String name) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/courses";
        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);

        if (type != null && !type.isEmpty()) {
            params.append("&type=").append(type);
        }
        if (name != null && !name.isEmpty()) {
            params.append("&name=").append(name);
        }
        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：专职思政课教师数量 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：专职思政课教师数量结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：专职思政课教师数量结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result coursesDetail(String year, int type, String name) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/coursesDetail";
        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);
        params.append("&type=").append(type);

        if (name != null && !name.isEmpty()) {
            params.append("&name=").append(name);
        }
        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：专职思政课教师数量详情 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：专职思政课教师数量详情结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：专职思政课教师数量详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result psychology(String year, String type, String name) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/psychology";
        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);

        if (type != null && !type.isEmpty()) {
            params.append("&type=").append(type);
        }
        if (name != null && !name.isEmpty()) {
            params.append("&name=").append(name);
        }
        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：心理健康教师数量 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：心理健康教师数量结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：心理健康教师数量结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result psychologyDetail(String year, int type, String name) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/psychologyDetail";
        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);
        params.append("&type=").append(type);

        if (name != null && !name.isEmpty()) {
            params.append("&name=").append(name);
        }
        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：心理健康教师数量详情 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：心理健康教师数量详情结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：心理健康教师数量详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result schoolNum(String year) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/schoolNum";
        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);

        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：各区县学校数量（包含高校） ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：各区县学校数量（包含高校）结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：各区县学校数量（包含高校）结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    @Override
    public Result teacherList(String year, String orgnazitionNo, int pageNum, int pageSize) {
        String url = ybtConfig.getUrl() + "/api/v1/data/teacherList";

        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);
        if (org.apache.commons.lang3.StringUtils.isNotBlank(orgnazitionNo)) {
            params.append("&orgnazitionNo=").append(orgnazitionNo);
        }
        params.append("&pageNum=").append(pageNum);
        params.append("&pageSize=").append(pageSize);

        String apiResult = HttpUtil.createPost(url + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：查询教师 ,url:{} params:{},apiResult:{}", url, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getObject("code", String.class))) {
            log.info("一表通：查询教师结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：查询教师结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result induction(String year, String type, String name) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/induction";
        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);

        if (type != null && !type.isEmpty()) {
            params.append("&type=").append(type);
        }
        if (name != null && !name.isEmpty()) {
            params.append("&name=").append(name);
        }

        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：年度新入职教师 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：年度新入职教师结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：年度新入职教师结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result inductionDetail(String year, int type, String name) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/inductionDetail";
        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);
        params.append("&type=").append(type);

        if (name != null && !name.isEmpty()) {
            params.append("&name=").append(name);
        }

        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：年度新入职教师详情 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：年度新入职教师详情结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：年度新入职教师详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result twoProfession(String year, String type, String name) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/twoProfession";
        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);

        if (type != null && !type.isEmpty()) {
            params.append("&type=").append(type);
        }
        if (name != null && !name.isEmpty()) {
            params.append("&name=").append(name);
        }
        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：职业院校双师型教师 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：职业院校双师型教师结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：职业院校双师型教师结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result twoProfessionDetail(String year, int type, String name) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/twoProfessionDetail";
        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);
        params.append("&type=").append(type);
        if (name != null && !name.isEmpty()) {
            params.append("&name=").append(name);
        }


        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：职业院校双师型教师详情 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：职业院校双师型教师详情结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：职业院校双师型教师详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result artistic(String year, String type, String name) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/artistic";
        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);

        if (type != null && !type.isEmpty()) {
            params.append("&type=").append(type);
        }
        if (name != null && !name.isEmpty()) {
            params.append("&name=").append(name);
        }

        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：艺体类教师 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：艺体类教师结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：艺体类教师结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result artisticDetail(String year, String type, String name) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/artisticDetail";
        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);

        if (type != null && !type.isEmpty()) {
            params.append("&type=").append(type);
        }
        if (name != null && !name.isEmpty()) {
            params.append("&name=").append(name);
        }

        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：艺体类教师详情 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：艺体类教师详情结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：艺体类教师详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result doctor(String year) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/doctor";
        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);

        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：普通本科高校专任教师博士占比 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：普通本科高校专任教师博士占比结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：普通本科高校专任教师博士占比结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result doctorDetail(String year) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/doctorDetail";
        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);

        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：普通本科高校专任教师博士占比详情 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：普通本科高校专任教师博士占比详情结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：普通本科高校专任教师博士占比详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result training(String year, String type, String name) {
        String apiUrl = ybtConfig.getUrl() + "/api/v1/data/training";
        StringBuilder params = new StringBuilder();
        params.append("?year=").append(year);

        if (type != null && !type.isEmpty()) {
            params.append("&type=").append(type);
        }
        if (name != null && !name.isEmpty()) {
            params.append("&name=").append(name);
        }

        String apiResult = HttpUtil.createPost(apiUrl + params.toString())
                .header(Header.AUTHORIZATION, TokenManager())
                .execute()
                .body();

        log.info("一表通：年度培训教师数量 ,url:{} params:{},apiResult:{}", apiUrl, params.toString(), apiResult);
        com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

        if ("0".equals(content.getString("code"))) {
            log.info("一表通：普通年度培训教师数量结果:{}", content.getString("data"));
            return Result.success(content.getObject("data", Object.class));
        } else {
            log.error("一表通：年度培训教师数量结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
            return Result.error(content.getString("msg"));
        }
    }

    public Result trainingDetail(String year, int type, String name) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/data/trainingDetail");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("type", String.valueOf(type));
            uriBuilder.addParameter("name", name);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            log.info("一表通：年度培训教师数量详情 ,url:{},params:{},apiResult:{}", url, uriBuilder, apiResult);

            com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：年度培训教师数量详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：年度培训教师数量详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：年度培训教师数量详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }

    public Result schoolDetail(String year, String orgnazitionNo) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/data/schoolDetail");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("orgnazitionNo", orgnazitionNo);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：学校详情结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：学校详情结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：学校详情请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }

    public Result kpiCity(String year, String type, String name) {
        Result result = null;
        try {
            URIBuilder uriBuilder = new URIBuilder(ybtConfig.getUrl() + "/api/v1/data/kpi");
            uriBuilder.addParameter("year", year);
            uriBuilder.addParameter("type", type);
            uriBuilder.addParameter("name", name);
            String url = uriBuilder.build().toString();

            String apiResult = HttpUtil.createPost(url)
                    .header(Header.AUTHORIZATION, TokenManager())
                    .header(Header.CONTENT_TYPE, "application/json")
                    .execute()
                    .body();

            com.alibaba.fastjson2.JSONObject content = com.alibaba.fastjson2.JSON.parseObject(apiResult);

            if ("0".equals(content.getString("code"))) {
                log.info("一表通：市级kpi结果:{}", content.getString("data"));
                result = Result.success(content.getObject("data", Object.class));
            } else {
                log.error("一表通：市级kpi结果异常 responseCode:{},responseMessage:{}", content.getString("code"), content.getString("msg"));
                result = Result.error(content.getString("msg"));
            }
        } catch (Exception e) {
            log.error("一表通：市级kpi请求异常 | 错误类型:{} | 详细信息:", e.getClass().getSimpleName(), e);
            result = Result.error(e.getMessage());
        }
        return result;
    }

    @Override
    public List<DistrictKpiDTO> kpi() {

        List<DistrictKpiVO> districtKpis = districtKpiMapper.countIndex();

        if (districtKpis == null) {
            return Collections.emptyList();
        }

        return districtKpis.stream().map(districtKpi -> {
            DistrictKpiDTO dto = new DistrictKpiDTO();
            dto.setDistrict(districtKpi.getDistrict());
            dto.setGradePercent(districtKpi.getGradePercent() != null ?
                    String.format("%.2f%%", districtKpi.getGradePercent().doubleValue()) : "");
            dto.setYjsPercent(districtKpi.getYjsPercent() != null ?
                    String.format("%.2f%%", districtKpi.getYjsPercent().doubleValue()) : "");
            dto.setBenKePercent(districtKpi.getBenKePercent() != null ?
                    String.format("%.2f%%", districtKpi.getBenKePercent().doubleValue()) : "");
            return dto;
        }).collect(Collectors.toList());
    }
}

